package com.study.jyg.rxjava2study.utils;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.study.jyg.rxjava2study.MyApplication;

public class ExitUtil {


    //定义一个变量，来标识是否退出
    private static boolean isExit = false;

    @SuppressLint("HandlerLeak")
    private static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    public static void exit(ExitListener exitListener) {
        if (!isExit) {
            isExit = true;
            Toast.makeText(MyApplication.getAppContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            //利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            exitListener.exit();
        }
    }

    public interface ExitListener {
        void exit();
    }
}
